package com.lau.config;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.lau.constant.SysConstant.MESSAGE;
import static com.lau.constant.SysConstant.SUCCESS;

/**
 * @author:Lau
 * @create: 2024-06-14 13:51
 * @Description:
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常，其他未处理的异常，按系统异常处理
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public JSONObject exceptionHandler(Exception e) {
        log.error("系统异常:{}", e.getMessage());
        JSONObject json = new JSONObject();
        json.put(SUCCESS, false);
        json.put(MESSAGE, e.getMessage());
        return json;
    }
}
